package com.haoshuang.zhouzhoubang.ui.page.shop.address

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityAddressManagementBinding
import com.haoshuang.zhouzhoubang.model.bean.AddressBean
import com.haoshuang.zhouzhoubang.ui.page.shop.address.add.AddAddressActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  地址管理界面
 */
class AddressManagementActivity2 :
    BaseVMActivity<AddressManagementViewModel, ActivityAddressManagementBinding>() {
    var list = arrayListOf<AddressBean>()

    val mAdapter = AddresManagementAdapter2(list)
    override fun initView(savedInstanceState: Bundle?) {
        val layoutManager = LinearLayoutManager(this)    // 创建布局管理者
        var type = intent.getIntExtra("type", 1)
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "地址管理"

            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = mAdapter
            llAddAddress.setOnClickListener { startPage<AddAddressActivity>("ids" to 0) }
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val intent = Intent()
                intent.putExtra("bean", list[position])
                setResult(11, intent)
                finish()
            }
        }
    }

    override fun onRestart() {
        super.onRestart()
        p = 1
        getNet()
    }

    override fun initData() {

        binding.apply {
            refreshLayout.setOnRefreshListener {
                p = 1
                getNet()
            }
            refreshLayout.setOnLoadMoreListener {
                p++
                getNet()
            }
            p = 1
            getNet()
        }
    }

    private fun getNet() {
        vm.getData("page" to p, "limit" to 10)
    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                toast("删除成功")
                p = 1
                getNet()
            }
        }
        vm.getData3.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                toast("设置成功")
                p = 1
                getNet()
            }
        }
        vm.getData.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
                binding.refreshLayout.setupEmptyData(list, mAdapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, list, mAdapter, p)
            }
        }
    }

}